/**
 * @author 刘超
 * @version 1.4.1
 * @Note 本插件采用 混合的构造函数/原型方式
 * @时间格式采用标准的格式 yyyy MM dd HH mm ss | 年 月 日 时 分 秒
 */
TimeUnit = function (){ // TimeUnit 类
    this.divId = "";         // div id
    this.divSuffix = "auto"; // 后缀 以便在页面有多个值时方便区分
    this.controlSpacing = "&nbsp;"; // 控件间距
    this.textSymbols = "";          // 控件和文字之间的符号 默认值是没有的
    this.DomInfo = { day : 0, hours : 0, minutes : 0, seconds : 0 }; // 保存数据
}
TimeUnit.prototype.DomIds = function (){ // 控件的ID
	var ids = {};
    ids.day = "txtDay" + this.divSuffix;
    ids.hours = "sHours" + this.divSuffix;
    ids.minutes = "sMinutes" + this.divSuffix;
    ids.seconds = "sSeconds" + this.divSuffix;

	return ids;
}
TimeUnit.prototype.txtDayBindEvent = function (){ // 给 文本框添加 事件
	var ids = this.DomIds();
	if(!SU.get(ids.day)){
		return;
	}
	SU.get(ids.day).onfocus = function (){ // 获得焦点事件
        if(this.value == "0"){
            this.value = "";
        }
    }
    SU.get(ids.day).onblur = function (){ // 失去焦点事件
        if(this.value == ""){
            this.value = 0;
        }
    }
    SU.get(ids.day).onkeydown = function (){ // 控制用户输入字符
		if(event.shiftKey && event.keyCode > 0){
            return event.returnValue=false;
		}
        if(event.keyCode != 8 && (event.keyCode < 48 || event.keyCode > 57) ){
            return event.returnValue=false;
        }
    }
}
TimeUnit.prototype.createSelect = function (fd){ // 生成控件[现在是字符串格式]

	if(arguments.length == 0){
		alert(this.Message.CreateSelect.argIsNull);
		return ;
	}

	if(arguments.length == 1){
		if(SU.type(fd) != "String" || !fd){ // 判断传入的是否是合法的日期格式字符串
			alert(this.Message.CreateSelect.fdIsError);
			return ;
		}

		var selectStr = ""; // 控件字符串变量
        var ids = this.DomIds(); // 存储了控件预定好的ID
        var pt  = this.PageText; // 页面上的字符

		if(fd.indexOf("dd") > -1){
            selectStr += "<input style='width:30px;'";
            selectStr += " id='"+ids.day+"' ";
            selectStr += " size='2' value='0' maxlength='3' style='margin-top:-5px;font-size:14px;' />&nbsp;";
            selectStr += this.textSymbols;
            selectStr += pt.day;
            selectStr += this.controlSpacing;
		}

		if(fd.indexOf("HH") > -1){
            selectStr += "<select style='width:50px;' id='"+ids.hours+"'>";
			for(var i=0; i<24; i++){
				var j = i<10?"0":"";
                selectStr += "<option  value='"+i+"'>"+j+i+"</option>";
			}
            selectStr += "</select>&nbsp;";
            selectStr += this.textSymbols;
            selectStr += pt.hours;
            selectStr += this.controlSpacing;
		}

		if(fd.indexOf("mm") > -1){
            selectStr += "<select  style='width:50px;' id='"+ids.minutes+"'>";
            for(var i=0; i<60; i++){
            	var j = i<10?"0":"";
            	if(i<=10){
            		selectStr += "<option  value='"+i+"'>"+j+i+"</option>";
            	}else{
            		if(i%5==0){
            			selectStr += "<option  value='"+i+"'>"+j+i+"</option>";
                	}
            	}
            }
            selectStr += "</select>&nbsp;";
            selectStr += this.textSymbols;
            selectStr += pt.minutes;
            selectStr += this.controlSpacing;
		}

		if(fd.indexOf("ss") > -1){
            selectStr += "<select style='width:50px;' id='"+ids.seconds+"'>";
            for(var i=0; i<60; i++){
            	var j = i<10?"0":"";
            	if(i%5==0){
            		selectStr += "<option value='"+i+"'>"+j+i+"</option>";
            	}
            }
            selectStr += "</select>&nbsp;";
            selectStr += this.textSymbols;
            selectStr += pt.seconds;
            selectStr += this.controlSpacing;
		}

		if(!selectStr){
			alert(this.Message.CreateSelect.createFailure);
		}

		return selectStr;
	}

}
TimeUnit.prototype.Init = function (divId,fd,suffix){ // 初始化控件参数
    if(arguments.length == 0){
        alert(this.Message.Init.argIsNull);
        return ;
    }

    if(arguments.length < 3){
        alert(this.Message.Init.argIsNoMatch);
        return ;
    }

    this.divId = divId;
	this.divSuffix = suffix;
    try {
		var selectStr = this.createSelect(fd);
		var div = SU.get(divId);

		div.innerHTML = selectStr; // 生成控件

		this.txtDayBindEvent();
	}catch(e){
		alert(this.Message.Init.initFailure);
		return false;
	}

	return true; // 成功
}
TimeUnit.prototype.Reversal = function (divId,fd,suffixs,reversal){ // 回显显示
    if(arguments.length == 0){
        alert(this.Message.Reversal.argIsNull);
        return ;
    }

	if(arguments.length < 4){
        alert(this.Message.Reversal.argIsNoMatch);
        return ;
    }

    this.Init(divId,fd,suffixs);
    if(!this.Decomposition(reversal)){return;}

    var ids = this.DomIds();
	if(fd.indexOf("dd") > -1){
	    SU.get(ids.day).value = this.DomInfo.day;
    }
    if(fd.indexOf("HH") > -1){
	    SU.get(ids.hours).value = this.DomInfo.hours;
    }
    if(fd.indexOf("mm") > -1){
	    SU.get(ids.minutes).value = this.DomInfo.minutes;
    }
    if(fd.indexOf("ss") > -1){
	    SU.get(ids.seconds).value = this.DomInfo.seconds;
    }
}
TimeUnit.prototype.Decomposition = function (reversal){ // 分解 秒数

	var s = parseInt(reversal);
    if(s < 0 || isNaN(s)){
        alert("reversal:"+reversal+"  "+this.Message.Reversal.reversalIsError);
        return ;
    }

    if(s == 0){
        return ;
    }
    // ////////////////////// 正在进行回显操作方法
    if(s >= 86400){
        this.DomInfo.day = parseInt(s/86400);
        s = s - this.DomInfo.day * 86400;
    }
    if(s >= 3600){
       this.DomInfo.hours = parseInt(s/3600);
       s = s - this.DomInfo.hours * 3600;
    }
    if(s >= 60){
       this.DomInfo.minutes = parseInt(s/60);
       s = s - this.DomInfo.minutes * 60;
    }
    this.DomInfo.seconds = s;

	return true;
}
TimeUnit.prototype.Description = function (reversal){ // 描述信息
    if(!this.Decomposition(reversal)){return "";}

    var desStr = "";
	var TimeSpacing = "&nbsp;"; // 时间字符串之间的间距
	var boo = false; // 前面是否要加空格 真为加 假为不加

    var pt  = this.DescriptionText; // 页面上的字符
    var isEN = pt.language=="en"?" ":"";
    if(this.DomInfo.day > 0){
		if(!boo){
            boo = true;
        }
        desStr += this.DomInfo.day + isEN + pt.day;
    }
    if(this.DomInfo.hours > 0){
		if(boo){
			desStr += TimeSpacing;
		}else{
			boo = true;
		}
        desStr += this.DomInfo.hours + isEN + pt.hours;
    }
    if(this.DomInfo.minutes > 0){
		if(boo){
            desStr += TimeSpacing;
        }else{
            boo = true;
        }
        desStr += this.DomInfo.minutes + isEN + pt.minutes;
    }
    if(this.DomInfo.seconds > 0){
		if(boo){
            desStr += TimeSpacing;
        }else{
            boo = true;
        }
        desStr += this.DomInfo.seconds + isEN + pt.seconds;
    }

	return desStr;
}
TimeUnit.prototype.Results = function (){ // 返回值
	var ids = this.DomIds();
	var s = 0;
	if(SU.get(ids.day)){
        this.DomInfo.day = parseInt(SU.get(ids.day).value);
		s += this.DomInfo.day*86400;
    }
    if(SU.get(ids.hours)){
        this.DomInfo.hours = parseInt(SU.get(ids.hours).value);
		s += this.DomInfo.hours*3600;
    }
    if(SU.get(ids.minutes)){
        this.DomInfo.minutes = parseInt(SU.get(ids.minutes).value);
        s += this.DomInfo.minutes*60;
    }
    if(SU.get(ids.seconds)){
        this.DomInfo.seconds = parseInt(SU.get(ids.seconds).value);
        s += this.DomInfo.seconds;
    }

    return s;
}
/**
 * 简单的对外对象
 * 
 * @param {Object}
 *            id
 * @param {Object}
 *            fd
 */
SimpleTimeUnit = function (id,fd){
    var su = new TimeUnit();
	/**
	 * 整合 克隆描述和时间描述 integration : 整合
	 * 
	 * @param {Object}
	 *            ctStr 克隆表达式
	 * @param {Object}
	 *            connStr 连接字符串
	 * @param {Object}
	 *            tuStr 时间表达式
	 */
    this.intCro = function (ctStr,connStr,tuStr){
        try{
            var str = "";
			str += CronParser(ctStr);
			str += " " + connStr + " ";
			str += this.description(tuStr);

			return str;
		}catch(e){
            return "";
		}
	}
    this.description = function (reversal,af,ab){ // 返回一个描述字符串
        var desStr = su.Description(reversal);
        if(typeof(af) != "undefined"){
            desStr = af + desStr;
        }
        if(typeof(ab) != "undefined"){
            desStr = desStr + ab;
        }

        return desStr;
    }

	if(arguments.length == 0){ // 只向用户提供 描述功能
        return ;
    }

	var divId = id;
	var divSuffix = id;
	var divFd = "dd HH:mm:ss";
	var disable = false; // 标志着控件是否被禁用

	if(arguments.length == 2){
        divFd = fd;
    }

	this.getTU = function (){
		return su;
	}

	this.show = function (){ // 初始化一个控件
        su.Init(divId,divFd,divSuffix);
		return this;
	}
    this.reversal = function (reversal){ // 初始化一个带值的控件
		su.Reversal(divId,divFd,divSuffix,reversal);
		return this;
	}

	// '<bean:message key="event.desc.duration.positive.int" />'
	// 返回值必须大于0 的 词条
    this.results = function (){ // 返回控件的值
        if(!disable){ // 如果控件被禁用了 那么就返回一个空值
	        return su.Results();
		}else{
            return 0;
		}
	}
    this.showDes = function (reversal,af,ab){ // 将描述信息打印在一个对象上
        var desStr = this.description(reversal,af,ab);
        SU.get(id).innerHTML = desStr;
    }

	this.setDisable = function (boo){ // 是否禁用控件
		disable = boo;
		SU.get(id).disabled = boo;
		if(SU.get("txtDay" + divSuffix)){
			SU.get("txtDay" + divSuffix).disabled = boo;
		}
	}
	this.setDayDisable = function (boo){ // 是否禁用"天"控件
	    var day = SU.get("txtDay" + divSuffix);
        if(boo){
            day.disabled = "disabled";
        }else{
            day.disabled = "";
		}

		return day;
    }

	// 添加必填项符号
	this.isRequiredSymbol = function (){
		var appStr = '<span style="color:red;">*</span>';
        SU.get(divId).innerHTML += appStr;
		su.txtDayBindEvent(); // 重新绑定文本框的事件
	}
}

